﻿using GalaSoft.MvvmLight.Command;
using GalaSoft.MvvmLight.Messaging;
using HuiZe.education.Common;
using HuiZe.education.Model;
using HuiZe.education.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace HuiZe.education.ViewModel
{
    public class AttendanceViewModel : ViewModelBase
    {
        public string  StudentNumber { get; set; }
        public int kcb { get; set;}
        private string tmToken = "9d8f366b-37c0-49b3-bfd6-ddb88b94aa16";
        private int tmlog_id = 676;

        public string StudentAllString { get; set;}
        private AttendUserModel currUserInfo = new AttendUserModel();
        private List<AttendUserModel> attendUserList = new List<AttendUserModel> {};
  
        public AttendUserModel CurrUserInfo
        {
            get { return currUserInfo; }
            set { currUserInfo = value; RaisePropertyChanged("CurrUserInfo"); }
        }
        public  AttendanceViewModel()
        {

            Register();
        }
        #region 委托消息
        public void Register()
        {
            //上一条
            Messenger.Default.Register<int>(0, "UpdateAdd", a =>
            {
                if (attendUserList.Count > 0)
                {
                    AttendUserModel attModel =  attendUserList.Where(r => r.UserId > CurrUserInfo.UserId).FirstOrDefault();
                    if (attModel != null)
                    {
                        SetUserInfo(attModel);//当前显示
                    }
                    else
                    {
                        ShowDialogView("已经是第一个学生了");
                    }
                 
                }
                
            });

            Messenger.Default.Register<int>(0, "UpdateDel", a =>
            {
                if (attendUserList.Count > 0)
                {
                    AttendUserModel attModel = attendUserList.Where(r => r.UserId > CurrUserInfo.UserId).FirstOrDefault();
                    if (attModel != null)
                    {
                        SetUserInfo(attModel);//当前显示
                    }
                    else
                    {
                        ShowDialogView("已经是最后一个学生了");
                    }

                }
            });
        }
        #endregion
        public void kcgd(int a)
        {
            kcb = a;
            classbegins(a.ToString());
            GetStudentList();
         
        }

        #region 上一条考勤信息
        public void AddUserInfo()
        {
            if (attendUserList.Count > 0)
            {
                AttendUserModel attModel = attendUserList.Where(r => r.Id > CurrUserInfo.Id).FirstOrDefault();
                if (attModel != null)
                {
                    SetUserInfo(attModel);//当前显示
                }
                else
                {
                    ShowDialogView("已经是第一个学生了");
                }

            }
        }
        #endregion

        #region 修改考勤状态
        /// <summary>
        /// 修改考勤状态
        /// </summary>
        public void EditStudentLog(int status)
        {

            ReqEditStuLog model = new ReqEditStuLog()
            {
                token = tmToken, // HuiZeeducation.Instance.UserInfo.token,
                log_id = tmlog_id,
                status = status,
                student_id = CurrUserInfo.Id,
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("//api/student/editStudentLog", data, "修改考勤状态");
            if (result.Item1)
            {
                ShowDialogView("考勤成功！");
            }
            else
            {
                ShowDialogView("考勤失败！");
            }

        }
        #endregion

        #region 下一条考勤信息
        public void DelUserInfo()
        {
            if (attendUserList.Count > 0)
            {
                AttendUserModel attModel = attendUserList.Where(r => r.Id < CurrUserInfo.Id).LastOrDefault();
                if (attModel != null)
                {
                    SetUserInfo(attModel);//当前显示
                }
                else
                {
                    ShowDialogView("已经是最后一个学生了");
                }

            }
        }
        #endregion
        #region 获取是否上课
        public void classbegins(string bkId)
        {

            ReqBkClassDetailModel model = new ReqBkClassDetailModel()
            {
                token = HuiZeeducation.Instance.UserInfo.token,

            };

            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("/api/resources/classbegins", data, "备课资源详情");
            if (result.Item1)
            {
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }

        }
        #endregion

        #region 一键考勤
        /// <summary>
        /// 一键考勤
        /// </summary>
        public void StudentoneCheck()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = tmToken,
                log_id = tmlog_id,
                grades_id = 15
            };
            string data = JsonHelper.ToJson(model);
            var result = Request<ResBkBeginClassMainModel>("//api/student/oneCheck", data, "一键考勤");
            if (result.Item1)
            {
                ShowDialogView("考勤成功！");
            }
            else
            {
                ShowDialogView("考勤失败！");
            }

        }
        #endregion

        #region 显示考勤状态
        /// <summary>
        /// 	显示考勤状态
        /// </summary>
        public List<AttendUserModel> GetstudentLog()
        {

            ReqStuListModel model = new ReqStuListModel()
            {
                token = tmToken, // HuiZeeducation.Instance.UserInfo.token,
                log_id = tmlog_id,
                grades_id = 15
            };
            List<AttendUserModel> CurrKQList = new List<AttendUserModel> { };
            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResStudentLog>>("/api/student/studentLogList", data, "考勤结果");
            if (result.Item1)
            {
                foreach (var amodel in result.Item2.data)
                {
                    AttendUserModel attendUserModel = new AttendUserModel();
                    attendUserModel.Id = amodel.id;
                    attendUserModel.UserId = amodel.user_id;
                    attendUserModel.UserName = amodel.name;
                    attendUserModel.UserType = amodel.status;
                    attendUserModel.UpdateTime = ConvertLongToDateTime(Convert.ToInt32(amodel.updatetime));
                    CurrKQList.Add(attendUserModel);
                }
            }
            else
            {
                //return null;
            }
            return CurrKQList;
        }
        #endregion

        public void GetStudentInfo()
        {
        
            if (attendUserList.Count > 0)
            {
               StudentAllString = "";
               foreach (var item in attendUserList)
                {
                    StudentAllString += item.UserName + "   ";
                }
            }

        }
        public List<AttendUserModel> attList()
        {
            List<AttendUserModel> CurrKQList= GetstudentLog();
            if (CurrKQList.Count > 0)
            {
                return CurrKQList;
            }
            else
            {
                return null;
            }
                
        }
        #region 获取学生列表
        public void GetStudentList()
        {

            tmToken = HuiZeeducation.Instance.UserInfo.token;
            int gradesId = HuiZeeducation.Instance.UserInfo.grades_id;
            ReqStuListModel model = new ReqStuListModel()
            {
                token = tmToken, //HuiZeeducation.Instance.UserInfo.token,
                log_id =tmlog_id, //kcb,
                grades_id = 15 // HuiZeeducation.Instance.UserInfo.grades_id
            };

            string data = JsonHelper.ToJson(model);
            var result = Request<List<ResStudentList>>("/api/student/studentList", data, "考勤学生列表");
            if (result.Item1)
            {
             
                if (result.Item2.data.Count > 0)
                {
                    foreach(var amodel in result.Item2.data)
                    {
                        AttendUserModel attendUserModel = new AttendUserModel();
                        attendUserModel.Id = amodel.id;
                        attendUserModel.UserId = amodel.user_id; 
                        attendUserModel.UserName= amodel.name;
                        attendUserModel.UpdateTime = ConvertLongToDateTime(amodel.updatetime);
                        attendUserList.Add(attendUserModel);
                    }
                    attendUserList= attendUserList.OrderBy(r=>r.Id).ToList();
                    SetUserInfo(attendUserList[0]);//获取默认显示用户
                    StudentNumber = result.Item2.data.Count().ToString();
                }
                //return result.Item2.data.resource_list;
                //ShowDialogView("删除成功!");
            }
            else
            {
                //return null;
            }
        }

        public void SetUserInfo(AttendUserModel resModel)
        {
            currUserInfo.Id = resModel.Id;
            currUserInfo.UserName = resModel.UserName;
            currUserInfo.UserId = resModel.UserId;
            currUserInfo.UpdateTime = resModel.UpdateTime;
        }
        #endregion

        #region 日期转换

        /// <summary>
        /// long转datetime
        /// </summary>
        /// <param name="d"></param>
        /// <returns></returns>
        public  DateTime ConvertLongToDateTime(long d)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(d + "0000");
            TimeSpan toNow = new TimeSpan(lTime);
            DateTime dtResult = dtStart.Add(toNow);
            return dtResult;
        }

        /// <summary>
        /// Datetime转long
        /// </summary>
        /// <param name="dt"></param>
        /// <returns></returns>
        public static long ConvertDateTimeToLong(DateTime dt)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            TimeSpan toNow = dt.Subtract(dtStart);
            long timeStamp = toNow.Ticks;
            timeStamp = long.Parse(timeStamp.ToString().Substring(0, timeStamp.ToString().Length - 4));
            return timeStamp;
        }
        #endregion
        #region 上一条
        public RelayCommand PreviousClickCommand
        {
            get
            {
                return new RelayCommand(new Action(() =>
                {

                }));
            }
        }
        #endregion
    }
    public class AttendUserModel : ViewModelBase
    {
        private int id;
        /// <summary>
        /// 用户ID
        /// </summary>
        public int Id
        {
            get { return id; }
            set
            {
                id = value; RaisePropertyChanged(() => Id);
            }
        }
        private string userName;
        /// <summary>
        /// 用户名称
        /// </summary>
        public string UserName
        {
            get
            {

                return userName;
            }
            set { userName = value; RaisePropertyChanged(() => UserName); }
        }
        private string userType;
        /// <summary>
        /// 考勤类型
        /// </summary>
        public string UserType
        {
            get { return userType; }
            set { userType = value; RaisePropertyChanged(() => UserType); }
        }
        private int userId;
        /// <summary>
        /// 用户ID
        /// </summary>
        public int UserId { get { return userId; } set { userId = value; RaisePropertyChanged(() => UserId); } }

        /// <summary>
        /// 更新时间
        /// </summary>
        private DateTime updateTime;
        public DateTime UpdateTime
        {
            get { return updateTime; }
            set { updateTime = value; RaisePropertyChanged(() => UpdateTime); }
        }


    }
}
